# Maintainer: Christian Hesse <mail@eworm.de>
# Contributor: Alexandre Boily <alexandreboily@gmail.com>
# Contributor: Illarion Kovalchuk <illarion.kovalchuk@gmail.com>
# Contributor: totoloco <totoloco at gmail _dot_com>
# Contributor: Ionut Biru <ibiru@archlinux.org>

# ALARM: Kevin Mihelich <kevin@archlinuxarm.org>
#  - use our cxxflags plus -fsigned-char
#  - patch to fix build on 32-bit
#  - remove -Wno-deprecated-copy flag from CMakeLists.txt

highmem=1
buildarch=12

pkgname=mysql-workbench
pkgver=8.0.24
pkgrel=2
_mysql_version=${pkgver}
_connector_version=${pkgver}
_gdal_version=3.2.2
_boost_version=1.73.0
_antlr4_version=4.9.2
pkgdesc='A cross-platform, visual database design tool developed by MySQL'
arch=('x86_64')
url='https://www.mysql.com/products/workbench/'
license=('GPL2')
depends=('cairo' 'antlr4-runtime' 'curl' 'desktop-file-utils' 'freetype2' 'giflib'
         'gtkmm3' 'hicolor-icon-theme' 'json-c' 'libgl' 'libsecret' 'libssh'
         'libxml2' 'libzip' 'pcre' 'proj' 'python' 'rapidjson' 'unixodbc'
         'vsqlite++' 'zstd')
makedepends=('cmake' 'boost' 'mesa' 'swig' 'java-runtime' 'imagemagick' "antlr4=${_antlr4_version}")
validpgpkeys=('A4A9406876FCBD3C456770C88C718D3B5072E1F5') # MySQL Release Engineering <mysql-build@oss.oracle.com>
source=("https://cdn.mysql.com/Downloads/MySQLGUITools/mysql-workbench-community-${pkgver}-src.tar.gz"{,.asc}
	"https://cdn.mysql.com/Downloads/MySQL-${_mysql_version%.*}/mysql-${_mysql_version}.tar.gz"{,.asc}
	"https://cdn.mysql.com/Downloads/Connector-C++/mysql-connector-c++-${_connector_version}-src.tar.gz"{,.asc}
	"https://download.osgeo.org/gdal/${_gdal_version}/gdal-${_gdal_version}.tar.xz"
	"https://downloads.sourceforge.net/project/boost/boost/${_boost_version}/boost_${_boost_version//./_}.tar.bz2"
	'0001-mysql-workbench-no-check-for-updates.patch'
	'0002-disable-unsupported-operating-system-warning.patch'
	'0001-fix-buiild-for-32-bit.patch'
	'atomic.patch'
	'arch_linux_profile.xml')
sha256sums=('1a482b7033e05f927e03017cc91a3a22dbea3a8717f96ac36c2239726c8d5e8f'
            'SKIP'
            '9b3a39a5951877e45e9a7536071dc503cd2b623facdc2ba91dcccf9fbea8d959'
            'SKIP'
            '5a2058477e5abab7942cbd796fa6824dc4ad11c4e09744899376f2a5b9857954'
            'SKIP'
            'a7e1e414e5c405af48982bf4724a3da64a05770254f2ce8affb5f58a7604ca57'
            '4eb3b8d442b426dc35346235c8733b5ae35ba431690e38c6a8263dce9fcbb402'
            'cdf687f23bc6e8d52dbee9fa02b23d755e80f88476f0fc2e7c4c71cdfed3792f'
            '2d0f6dcf38f22e49ef7ab9de0230484f1ffac41b7ac40feaf5ef4538ae2f7a18'
            '719501bbd1de673767007c429feed2fc48d1176d456161c4ba69cf3165c0438a'
            '17294a67637ab7ffff5c39262208e63d21acac72cc2492f616ef1d8e0ae9ac02'
            '3a59b46ac2e7c6a0a72733d71ca33ec85146e8399a3f23871cb3a965cd8e749e')

prepare() {
	cd "${srcdir}/mysql-workbench-community-${pkgver}-src/"

	# Disable 'Help' -> 'Check for Updates'
	# Updates are provided via Arch Linux packages
	patch -Np1 < "${srcdir}"/0001-mysql-workbench-no-check-for-updates.patch

	# disable unsupported operating system warning
	patch -Np1 < "${srcdir}"/0002-disable-unsupported-operating-system-warning.patch

	# GCC 7.x introduced some new warnings, remove '-Werror' for the build to complete
	sed -i '/^set/s|-Werror -Wall|-Wall|' CMakeLists.txt

	# GCC 7.x complains about unsupported flag
	sed -i 's|-Wno-deprecated-register||' ext/scintilla/gtk/CMakeLists.txt

	# disable stringop-truncation for GCC 8.x
	sed -i '/^set/s|-Wall|-Wall -Wno-stringop-truncation|' CMakeLists.txt

	# make sure to link against bundled libraries
	sed -i "/target_link_libraries/s|\\$|-L${srcdir}/install-bundle/usr/lib/ \\$|" backend/wbpublic/CMakeLists.txt

	# be happy with more recent antlr
	sed -i "/set(ANTLR_JAR_FILENAME/s/4.9.1/${_antlr4_version}/" CMakeLists.txt

	# remove -Wno-deprecated-copy
	sed -i 's|-Wno-deprecated-copy||' CMakeLists.txt

	# fix build with 32-bit
	[[ $CARCH != "aarch64" ]] && patch -Np1 < "${srcdir}"/0001-fix-buiild-for-32-bit.patch

	# link v6 against libatomic
	cd "${srcdir}/mysql-${_mysql_version}"
	if [[ $CARCH == "armv6h" ]]; then
		patch -Np0 < "${srcdir}"/atomic.patch
	fi
}

build() {
	# Build mysql
	mkdir "${srcdir}/mysql-${_mysql_version}-build"
	cd "${srcdir}/mysql-${_mysql_version}-build"
	echo "Configure mysql..."
	cmake "${srcdir}/mysql-${_mysql_version}" \
		-DWITHOUT_SERVER=ON \
		-DBUILD_CONFIG=mysql_release \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DSYSCONFDIR=/etc/mysql \
		-DMYSQL_DATADIR=/var/lib/mysql \
		-DWITH_BOOST="${srcdir}/boost_${_boost_version//./_}"
	echo "Build mysql..."
	make
	echo "Install mysql..."
	make DESTDIR="${srcdir}/install-bundle/" install

	# Build mysql-connector-c++
	mkdir "${srcdir}/mysql-connector-c++-${_connector_version}-src-build"
	cd "${srcdir}/mysql-connector-c++-${_connector_version}-src-build"
	echo "Configure mysql-connector-c++..."
	cmake "${srcdir}/mysql-connector-c++-${_connector_version}-src" \
		-Wno-dev \
		-DCMAKE_INSTALL_PREFIX=/usr \
		-DCMAKE_BUILD_TYPE=Release \
		-DINSTALL_LIB_DIR=lib \
		-DMYSQL_DIR="${srcdir}/install-bundle/" \
		-DMYSQL_CONFIG_EXECUTABLE="${srcdir}/install-bundle/usr/bin/mysql_config" \
		-DWITH_JDBC=ON
	echo "Build mysql-connector-c++..."
	make
	echo "Install mysql-connector-c++..."
	make DESTDIR="${srcdir}/install-bundle/" install

	# Build gdal
	cd "${srcdir}/gdal-${_gdal_version}"
	echo "Configure gdal..."
	./configure \
		--prefix=/usr \
		--includedir=/usr/include/gdal \
		--with-sqlite3 \
		--with-mysql="${srcdir}/install-bundle/usr/bin/mysql_config" \
		--with-curl \
		--without-jasper
	echo "Build gdal..."
	make LD_LIBRARY_PATH="${srcdir}/install-bundle/usr/lib/"
	echo "Install gdal..."
	make LD_LIBRARY_PATH="${srcdir}/install-bundle/usr/lib/" DESTDIR="${srcdir}/install-bundle/" install

	# Build MySQL Workbench itself with bundled libs
	mkdir "${srcdir}/mysql-workbench-community-${pkgver}-src-build"
	cd "${srcdir}/mysql-workbench-community-${pkgver}-src-build"
	echo "Configure mysql-workbench..."
	cmake "${srcdir}/mysql-workbench-community-${pkgver}-src" \
		-Wno-dev \
		-DCMAKE_INSTALL_PREFIX:PATH=/usr \
		-DCMAKE_CXX_FLAGS="${CXXFLAGS} -std=c++17 -fsigned-char" \
		-DCMAKE_BUILD_TYPE=Release \
		-DMySQL_CONFIG_PATH="${srcdir}/install-bundle/usr/bin/mysql_config" \
		-DMySQLCppConn_LIBRARY="${srcdir}/install-bundle/usr/lib/libmysqlcppconn.so" \
		-DMySQLCppConn_INCLUDE_DIR="${srcdir}/install-bundle/usr/include/jdbc" \
		-DGDAL_INCLUDE_DIR="${srcdir}/install-bundle/usr/include" \
		-DGDAL_LIBRARY="${srcdir}/install-bundle/usr/lib/libgdal.so" \
		-DWITH_ANTLR_JAR='/usr/share/java/antlr-complete.jar' \
		-DUSE_BUNDLED_MYSQLDUMP=1
	echo "Build mysql-workbench..."
	make
}

package() {
	# install bundled libraries
	for LIBRARY in $(find "${srcdir}/install-bundle/usr/lib/" -type f -regex '.*/lib\(gdal\|mysql\(client\|cppconn\)\)\.so\..*'); do
		BASENAME="$(basename "${LIBRARY}")"
		SONAME="$(readelf -d "${LIBRARY}" | grep -Po '(?<=(Library soname: \[)).*(?=\])')"
		install -D -m0755 "${LIBRARY}" "${pkgdir}"/usr/lib/mysql-workbench/"${BASENAME}"
		ln -s "${BASENAME}" "${pkgdir}"/usr/lib/mysql-workbench/"${SONAME}"
	done

	# install bundled mysql and mysqldump
	install -m0755 "${srcdir}/install-bundle/usr/bin/mysql"{,dump} "${pkgdir}"/usr/lib/mysql-workbench/

	# install MySQL Workbench itself
	cd "${srcdir}/mysql-workbench-community-${pkgver}-src-build"

	make DESTDIR="${pkgdir}" install

	# icons
	for SIZE in 16 24 32 48 64 96 128; do
		# set modify/create for reproducible builds
		convert -scale ${SIZE} +set date:create +set date:modify \
			"${srcdir}/mysql-workbench-community-${pkgver}-src/images/icons/MySQLWorkbench-128.png" \
			"${srcdir}/mysql-workbench.png"
		install -D -m0644 "${srcdir}/mysql-workbench.png" "${pkgdir}/usr/share/icons/hicolor/${SIZE}x${SIZE}/apps/mysql-workbench.png"
	done

	install -D -m 0644 "${srcdir}"/arch_linux_profile.xml \
		"${pkgdir}"/usr/share/mysql-workbench/mysql.profiles/Arch_Linux_\(MariaDB\).xml
}

